package com.zy.fcs.service;

import java.util.ArrayList;
import java.util.List;

import com.zy.fcs.domain.TblMediaManagement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zy.fcs.bean.ZtreeNode;
import com.zy.fcs.domain.TblPcmDoccde;
import com.zy.fcs.domain.TblPcmDoctyp;
import com.zy.fcs.repository.TblMediaManagementRepository;
import com.zy.fcs.repository.TblPcmDoccdeRepository;
import com.zy.fcs.repository.TblPcmDoctypRepository;
import com.zy.fcs.service.oss.OssService;

/**
 * 树形结构操作
 *
 * @author zhanghengchang
 * @date 2018/3/6
 */
@Service
public class ZtreeService {

    private static Logger logger = LoggerFactory.getLogger(ZtreeService.class);

    @Autowired
    private TblPcmDoctypRepository tblPcmDoctypRepository;

    @Autowired
    private TblPcmDoccdeRepository tblPcmDoccdeRepository;

    @Autowired
    private OssService ossService;

    @Autowired
    TblMediaManagementRepository tblMediaManagementRepository;

    /**
     * 取得树形结构数据 机构号/申请号/身份证/身份证正面/1.jpg
     *
     * @param instNo 机构号
     * @param orderNo 订单号
     * @return Ztree树形json
     */
    public JSONArray getTreeJson(String instNo, String orderNo) throws Exception {

        // 证明材料类别表
        List<TblPcmDoctyp> doctypList = tblPcmDoctypRepository.findByDtInstNo(instNo);
        // 证明文件代码表
        List<TblPcmDoccde> doccdeList = tblPcmDoccdeRepository.findByDcInstNo(instNo);

        List<TblMediaManagement> medias = new ArrayList<>();
        if (orderNo != null && !"".equals(orderNo)) {
            // 从OSS获取该订单下所有图片影像Url信息
            medias = tblMediaManagementRepository.findByInstNoAndOrderNo(instNo, orderNo);
        } else {
            orderNo = "订单号";
        }

        // Tree
        String rootUri = instNo + "/" + orderNo + "/";
        ZtreeNode ztree = new ZtreeNode(rootUri, rootUri, orderNo, true, true);

        List<ZtreeNode> doctypNodeList = new ArrayList<>();
        for (TblPcmDoctyp doctyp : doctypList) {
            String typeUri = rootUri + doctyp.getDtDocType() + "/";
            String typeUriDisp = orderNo + "/" + doctyp.getDtDesc() + "/";
            ZtreeNode doctypeNode = new ZtreeNode(typeUri, typeUriDisp, doctyp.getDtDesc(), true, true);
            List<ZtreeNode> doccdeNodeList = new ArrayList<>();
            for (TblPcmDoccde doccde : doccdeList) {
                if (doccde.getDcDocType().equals(doctyp.getDtDocType())) {
                    String cdeUri = typeUri + doccde.getDcDocCd() + "/";
                    String cdeUriDisp = typeUriDisp + doccde.getDcDocDescription() + "/";
                    ZtreeNode doccdeNode = new ZtreeNode(cdeUri, cdeUriDisp, doccde.getDcDocDescription(), true, true);

                    List<ZtreeNode> mediaList = new ArrayList<>();
                    for (TblMediaManagement media : getMedisNodes(medias, cdeUri)) {
                        String url = media.getMediaUrl();
                        String mediaUriDisp = cdeUriDisp + getFileName(url);
                        mediaList.add(new ZtreeNode(media.getId(), url, mediaUriDisp, getFileName(url), false, false));
                    }
                    doccdeNode.setChildren(mediaList);
                    doccdeNodeList.add(doccdeNode);
                }
            }
            doctypeNode.setChildren(doccdeNodeList);
            doctypNodeList.add(doctypeNode);
        }
        ztree.setChildren(doctypNodeList);

        List<ZtreeNode> tree = new ArrayList<>();
        tree.add(ztree);
        return (JSONArray)JSONObject.toJSON(tree);
    }

    /**
     * 获得指定目录下的所有Url
     *
     * @param mediaList 订单下所有文件
     * @param prefix 指定目录
     * @return 指定目录下的所有文件
     */
    private List<TblMediaManagement> getMedisNodes(List<TblMediaManagement> mediaList, String prefix) {
        List<TblMediaManagement> ret = new ArrayList<>();

        for (TblMediaManagement media : mediaList) {
            if (media.getMediaUrl().startsWith(prefix)) {
                ret.add(media);
            }
        }

        return ret;
    }

    /**
     * 根据文件路径获得文件名
     *
     * @param url 文件Url
     * @return 文件名
     */
    private String getFileName(String url) {
        return url.substring(url.lastIndexOf("/") + 1);
    }

}
